home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / include / dynl.h next >
C/C++ Source or Header  |  1996-12-09  |  13KB  |  534 lines

  1.  
  2.  
  3.  
  4.  
  5. /*
  6.  *
  7.  *    Copyright (c) 1993-1996 Algorithms Corporation
  8.  *    3020 Liberty Hills Drive
  9.  *    Franklin, TN  37067
  10.  *
  11.  *    ALL RIGHTS RESERVED.
  12.  *
  13.  *
  14.  *
  15.  */
  16.  
  17. #ifndef    _DYNL_H
  18. #define    _DYNL_H
  19.  
  20. /*  What kind of machine?  */
  21.  
  22. #ifdef    _MSC_VER
  23. #if    _MSC_VER >= 900  &&  defined(_M_IX86)
  24. #define    MSC32
  25. #else
  26. #define MSC16
  27. #endif
  28. #endif
  29.  
  30. #ifdef    __WATCOMC__
  31. #ifdef    M_I86
  32. #define    WATCOM16
  33. #else
  34. #define WATCOM32
  35. #endif
  36. #endif
  37.  
  38.  
  39. #if    defined(__TURBOC__)  ||  defined(__BORLANDC__)
  40. #ifdef    __MSDOS__
  41. #define BC16
  42. #else
  43. #define BC32
  44. #endif
  45. #endif
  46.  
  47. #ifdef    __SC__        /*  Symantec C  */
  48. #ifdef    _WIN32
  49. #define    SYMC32
  50. #else
  51. #define    SYMC16
  52. #endif
  53. #endif
  54.  
  55.  
  56. #include <stdio.h>
  57. /*#include <malloc.h>*/
  58. #include <stdlib.h>
  59. #include <setjmp.h>
  60. #include <stdarg.h>
  61. #ifdef    __WATCOMC__
  62. #include <string.h>
  63. #endif
  64.  
  65. #ifdef    BOEHM_GC
  66. #include "gc.h"
  67. #define    malloc(x)    GC_malloc_ignore_off_page(x)
  68. #define    free(x)
  69. #define    calloc(n,s)    GC_malloc_ignore_off_page((n)*(s))
  70. #define    realloc(p,s)    GC_realloc(p,s)
  71. #endif
  72.  
  73.  
  74. #ifdef    RTKERNEL
  75. #ifndef    NATIVE_THREADS
  76. #define    NATIVE_THREADS
  77. #endif
  78. #include "dynrtk.h"
  79. #endif
  80.  
  81. #ifdef    NATIVE_THREADS
  82. typedef    CRITICAL_SECTION    CRITICALSECTION;
  83. #define    INITIALIZECRITICALSECTION(cs)    InitializeCriticalSection(&cs)
  84. #define    DELETECRITICALSECTION(cs)    DeleteCriticalSection(&cs)
  85. #define    ENTERCRITICALSECTION(cs)    EnterCriticalSection(&cs)
  86. #define    LEAVECRITICALSECTION(cs)    LeaveCriticalSection(&cs)
  87. #else
  88. typedef    int    CRITICALSECTION;
  89. #define    INITIALIZECRITICALSECTION(cs)    USE(cs)
  90. #define    DELETECRITICALSECTION(cs)    USE(cs)
  91. #define    ENTERCRITICALSECTION(cs)    USE(cs)
  92. #define    LEAVECRITICALSECTION(cs)    USE(cs)
  93. #endif
  94.  
  95. #define    NULLOBJ    ((object)0)
  96.  
  97. #if defined( _WINDOWS)  ||  defined(MSC32)  ||  defined(MSC16)
  98. #if defined(MSC32)  &&  !defined(WIN32)
  99. #define    WIN32
  100. #endif
  101. #include <windows.h>
  102. #include <windowsx.h>
  103. #include <sql.h>
  104. #ifndef    WIN32
  105. #include <ole2.h>
  106. #include <dispatch.h>
  107. //  redefine the following without the __export so I can use static functions
  108. #undef    STDMETHODIMP
  109. #undef    STDMETHODIMP_
  110. #define    STDMETHODIMP        HRESULT FAR CDECL
  111. #define    STDMETHODIMP_(t)    t FAR CDECL
  112. #endif
  113. #undef    NULL
  114. #define NULL    ((void *)0)
  115. #include "dynwin.h"
  116. #endif
  117.  
  118. #include "findfile.h"
  119.  
  120. #ifdef    __cplusplus
  121. extern "C"  {
  122. #endif
  123.  
  124. /*  objects must be declared volatile because if the variable is kept in a
  125.     register, it may not be garbage collected  */
  126.  
  127. #if    defined(MSC32)  ||  defined(BC32)  ||  (defined(unix) && defined(i386))
  128. typedef    struct    _Object_iv_t     *object;
  129. #else
  130. typedef    struct    _Object_iv_t     * volatile object;
  131. #endif
  132.  
  133. /*  objrtn is used to signify functions which return objects.  It is necessary
  134.     because some compilers (GNU) don't like function return types declared as
  135.     volatile.   */
  136.  
  137. typedef    struct    _Object_iv_t     *objrtn;
  138.  
  139. typedef    objrtn    (*ofun)();
  140. typedef    int    (*ifun)();
  141.  
  142. void    InitDynace(void *);
  143. void    InitKernel(void *, int);
  144. ofun    _FindMethod(object, object);
  145. ofun    _FindMethod2(object, object, int);
  146. objrtn    _jumpToMethod(ofun f);
  147. void    *GetIVptr(object, object);
  148. int    IsObj(object);
  149. void    InitGenerics(void);
  150. void    _IC_exec(object *);
  151.  
  152.  
  153. #define TRACE_DONT_CARE        0
  154. #define TRACE_OFF        1
  155. #define TRACE_ON        2
  156. #define TRACE_ALL        3
  157.  
  158. #define    _cat2_(a, b)    a##b
  159. #define _cat_(a, b)    _cat2_(a, b)
  160.  
  161. #define Generic(x)    _cat_(x, _g)
  162.  
  163.  
  164. #define InitClass(c)    c##_initialize()
  165.  
  166. #define ivPtr(x)    ((ivType *) GetIVptr((x), CLASS))
  167. #define ivsPtr        ivPtr(self)
  168. #define accessIVs    ivType *iv = ivsPtr
  169. #define accessIVsOf(x)    ivType *iv = ivPtr(x)
  170.  
  171. #define    GetIVs(c,x)    ((c##_iv_t *) GetIVptr((x), c##_c))
  172. #define    GetCVs(c)    ((c##_cv_t *) GetIVptr(c##_c, ClassOf(c##_c)))
  173.  
  174. #define ivMethodFor(c,g,m,f)    \
  175.     gNewMethod(Method_c, #m, c##_c, Generic(g), (ofun) m, (ofun) f)
  176. #define cvMethodFor(c,g,m,f)    \
  177.     gNewMethod(Method_c, #m, ClassOf(c##_c), Generic(g), (ofun) m, (ofun) f)
  178.  
  179. #define iMethodFor(c,g,m)    \
  180.     gNewMethod(Method_c, #m, c##_c, Generic(g), (ofun) m, (ofun) m)
  181. #define cMethodFor(c,g,m)    \
  182.     gNewMethod(Method_c, #m, ClassOf(c##_c), Generic(g), (ofun) m, (ofun) m)
  183.  
  184. #define ClassOf(x)    (object) *((object *) (x))
  185.  
  186. #define imeth        static
  187. #define cmeth        static
  188. #define ivmeth        static
  189. #define cvmeth        static
  190. #define END        (object) 0
  191.  
  192. #define IsInstanceOf(i,c)    (ClassOf(i) == (c))
  193. #define IsaMetaClass(x)        (ClassOf(x) == MetaClass_c)
  194. #define IsaClass(x)        (IsaMetaClass(ClassOf(x)) || IsaMetaClass(x))
  195.  
  196. #define EQ(a,b)        ((a) == (b))
  197. #define NEQ(a,b)    ((a) != (b))
  198.  
  199.  
  200. #define tFindMethod(t, c, g, l)  (*(t (*)()) _FindMethod2(c, g, l))
  201.  
  202.  
  203. /* get instance method from a class object */
  204. #define imcPointer(c,g)  (g##_t) gFindMethod(c, Generic(g), 1)
  205.  
  206. /* get class method from a class object */
  207. #define cmcPointer(c,g)  (g##_t) gFindMethod(ClassOf(c), Generic(g), 1)
  208.  
  209. /* get instance method from an instance object */
  210. #define imiPointer(i,g)  (g##_t) gFindMethod(ClassOf(i), Generic(g), 1)
  211.  
  212. /* get class method from an instance object */
  213. #define cmiPointer(i,g)  (g##_t) gFindMethod(ClassOf(ClassOf(i)), Generic(g), 1)
  214.  
  215. #define iSuper(c,g)    (*(g##_t)_FindMethod2(c, Generic(g), 2))
  216. #define cSuper(c,g)    (*(g##_t)_FindMethod2(ClassOf(c), Generic(g), 2))
  217. #define typedSuper(t,g)    (*tFindMethod(t, ClassOf(self), Generic(g), 2))
  218. #define typedGeneric(t,g)     (*(t (*)()) g)
  219.  
  220. #if 0
  221. #define oSuper(c, g, x)    \
  222.     ((ClassOf(ClassOf(x)) == MetaClass_c) ? cSuper(c, g) : iSuper(c, g))
  223. #else
  224. #define oSuper(c,g,x)    (*(g##_t)_FindMethod2((ClassOf(ClassOf(x)) == MetaClass_c) ? ClassOf(c) : c, Generic(g), 2))
  225. #endif
  226.     
  227. #define RespondsTo(i,g)    (gFindMethod(ClassOf(i), Generic(g), 1) != NULL)
  228.  
  229. #define RegisterVariable(v)    gRegisterMemory(Dynace_c, (void *)&v, (long) sizeof(object))
  230.  
  231.     
  232. extern    int    _CheckObjects_;
  233. extern    object    _LastGeneric_;        /*  last generic called  */
  234. extern    CRITICALSECTION  _CI_CS_;    /*  used during class initialization  */
  235.  
  236.  
  237. #define GetArg(type)    va_arg(_rest_, type)
  238.  
  239. #define ChkArg(obj, argn)                        \
  240.     if (_CheckObjects_  &&  !IsObj(obj))                \
  241.         gInvalidObject(_LastGeneric_, argn, self)
  242.  
  243. #define ChkArgTyp(obj, argn, argType)                    \
  244.     if (_CheckObjects_)  {                        \
  245.         if (!IsObj(obj))                    \
  246.             gInvalidObject(_LastGeneric_, argn, self);    \
  247.         if (argType)  {                        \
  248.             object    lg = _LastGeneric_;            \
  249.             if (!gIsKindOf(obj, argType))            \
  250.                 gInvalidType(lg, argn, self, argType, obj); \
  251.             _LastGeneric_ = lg;                \
  252.         }                            \
  253.     }
  254.  
  255. #define ChkArgNul(obj, argn)           if (obj)  ChkArg(obj, argn)
  256.  
  257. #define ChkArgTypNul(obj, argn, argType)     \
  258.     if (obj)  ChkArgTyp(obj, argn, argType)
  259.  
  260.  
  261. /*  Thread stuff  */
  262.  
  263.  
  264. /*  thread states   */
  265.  
  266. #define NEW_THREAD        0
  267. #define    RUNNING_THREAD        1
  268. #define HOLD_THREAD        2
  269. #define DONE_THREAD        3
  270. #define EXPUNGE_THREAD        4
  271. #define WAITING_FOR_THREAD    5
  272. #define WAITING_FOR_SEMAPHORE    6
  273.  
  274. #define DEFAULT_PRIORITY    100
  275.  
  276. extern    int    _tick_count;
  277. extern    int    _no_context_switch;
  278. extern    jmp_buf    _t_start;
  279.  
  280. extern    void    (*__dynace_yield)(void);
  281. extern    void    _start_threader(char *stkpos), _start_thread(void);
  282.  
  283. #define INHIBIT_THREADER    _no_context_switch++
  284. #define ENABLE_THREADER        if (_no_context_switch) _no_context_switch--
  285.  
  286. #if 0  /*  Debugging or to disable Threads  */
  287. #define YIELD    
  288. #else
  289. #define YIELD    if (!_tick_count  &&  !_no_context_switch)  (*__dynace_yield)()
  290. #endif
  291.  
  292. #define StartThreader(x)        \
  293.     _start_threader((char *) &x);    \
  294.     if (setjmp(_t_start))        \
  295.         _start_thread()
  296.  
  297.  
  298. /*  End of thread specific stuff  */
  299.  
  300. #ifndef    MSC32
  301. #define IGTYPE    static
  302. #else
  303. #define IGTYPE    static    __declspec(naked) 
  304. #endif
  305.  
  306. #if    !defined(MSC32)  &&  !defined(__alpha)
  307. #define GenericBody(g)   _jumpToMethod( _FindMethod(self, Generic(g)) )
  308. #endif
  309.  
  310. #ifdef    __alpha
  311. #define GenericBody(g)                  \
  312.     va_list args; va_start(args, self);             \
  313.     _jumpToMethod( _FindMethod(self, Generic(g)) )
  314. #endif
  315.  
  316. #ifdef    MSC32
  317. #define GenericBody(g)                    \
  318.     _asm {    push    DWORD PTR Generic(g) }        \
  319.     _asm {    push    DWORD PTR [esp+8] }        \
  320.     _asm {    call    _FindMethod     }        \
  321.     _asm {    add    esp, 8     }            \
  322.     _asm {    jmp    eax }
  323. #endif
  324.  
  325.  
  326.  
  327. #if 0
  328. #ifndef    __alpha
  329. #define defGeneric(t, g)                       \
  330.     object    Generic(g) = (object) 0;            \
  331.     static    t    _##g(object i, ...)            \
  332.         {                            \
  333.         _jumpToMethod( _FindMethod(i, Generic(g)) );     \
  334.     }                            \
  335.         t    (*g)(object i, ...) = _##g;
  336. #else
  337. #define defGeneric(t, g)                                        \
  338.         object  Generic(g) = (object) 0;                        \
  339.         static  t       _##g(object i, ...)                     \
  340.         {                                                       \
  341.                 va_list args; va_start(args, i);                \
  342.                 _jumpToMethod( _FindMethod(i, Generic(g)) );    \
  343.         }                                                       \
  344.         t       (*g)(object i, ...) = _##g;
  345. #endif
  346. #endif /* 0 */
  347.  
  348. #define externGeneric(t, g)                    \
  349.     extern    object    Generic(g);                \
  350.     extern    t    (*g)(object i, ...);
  351.  
  352.  
  353.  
  354. #define InitGeneric(x)    Generic(x) = gNewWithStr(GenericFunction_c, #x)
  355.  
  356.  
  357. #if 0 /* DPP_FASTWIDE  */
  358.  
  359. typedef    struct  {
  360.     object    fill1;
  361.     unsigned short fill2, fill3;
  362. }    *OHead;
  363.  
  364. #define    ClassID(c)        *(int *)((OHead)(c) + 1)
  365. #define    MethCache(g)        (*(ofun **)((OHead)Generic(g) + 1))
  366. #define    MethodPointer(g, obj)    MethCache(g)[ClassID(ClassOf(obj))]
  367.  
  368.  
  369. #if    DPP_STRATEGY > 2
  370. inline    void    gInvalidObject(object self, int argn, object arg1);
  371. #endif
  372.  
  373. #if    DPP_STRATEGY <= 2
  374. void    (*gInvalidObject)(object self, int argn, object arg1);
  375. #endif
  376.  
  377.  
  378. #define FW_GENERIC(g)                         \
  379.     if (_CheckObjects_)  {                    \
  380.         if (!IsObj(self))                \
  381.             gInvalidObject(Generic(g), 1, self);    \
  382.         _LastGeneric_ = Generic(g);            \
  383.     }                            \
  384.     YIELD;                            \
  385.     if (!(_meth_ = MethodPointer(g, self)))            \
  386.         MethodPointer(g, self) = _meth_ = _FindMethod(self, Generic(g))
  387.     
  388.  
  389. #endif
  390.  
  391. #if    DPP_FASTWIDE
  392.  
  393. typedef    struct  {
  394.     objrtn    fill1;
  395.     unsigned short fill2, fill3;
  396.     int    id;
  397. }    *CHead;
  398.  
  399. typedef    struct  {
  400.     objrtn    fill1;
  401.     unsigned short fill2, fill3;
  402.     ofun    *mc;
  403. }    *GHead;
  404.  
  405. #define    MethodPointer(g, obj)   \
  406.         ((GHead)Generic(g))->mc[((CHead)ClassOf(obj))->id]
  407.  
  408. #if    DPP_STRATEGY > 2
  409. inline    void    gInvalidObject(object self, int argn, object arg1);
  410. #endif
  411.  
  412. #if    DPP_STRATEGY <= 2
  413. void    (*gInvalidObject)(object self, int argn, object arg1);
  414. #endif
  415.  
  416. #ifndef    SMALL_FW
  417.  
  418. #define FW_GENERIC(g)                         \
  419.     if (_CheckObjects_)  {                    \
  420.         if (!IsObj(self))                \
  421.             gInvalidObject(Generic(g), 1, self);    \
  422.         _LastGeneric_ = Generic(g);            \
  423.     }                            \
  424.     YIELD;                            \
  425.     if (!(_meth_ = MethodPointer(g, self)))            \
  426.         MethodPointer(g, self) = _meth_ = _FindMethod(self, Generic(g))
  427.  
  428. #else
  429.  
  430. #define FW_GENERIC(g)                         \
  431.     if (!(_meth_ = MethodPointer(g, self)))            \
  432.         MethodPointer(g, self) = _meth_ = _FindMethod(self, Generic(g))
  433.  
  434. #endif    
  435.  
  436. #endif
  437.  
  438.  
  439.  
  440. #ifndef  Talloc
  441. extern    void    *Chkmem(void *, char *, int);
  442. #define     Talloc(t)   (t *) Chkmem(malloc(sizeof(t)), __FILE__, __LINE__)
  443. #define     Tcalloc(t)  (t *) Chkmem(calloc(1, sizeof(t)), __FILE__, __LINE__)
  444. #define     Tnalloc(t,n)   (t *) Chkmem(malloc((n) * sizeof(t)), __FILE__, __LINE__)
  445. #define     Tncalloc(t,n)  (t *) Chkmem(calloc((n), sizeof(t)), __FILE__, __LINE__)
  446. #define  Tnrealloc(t,n,b) (t *) Chkmem(realloc((void*)(b), (n) * sizeof(t)), __FILE__, __LINE__)
  447. #endif
  448.  
  449. #define    StackAlloc(c)    gStackAlloc(c, alloca(gInstanceSize(c)))
  450.  
  451. #if 0
  452. #if defined(__TURBOC__)  /*  ||  defined(unix)  */
  453. #include "generics.h"
  454. #else
  455. #include <generics.h>
  456. #endif
  457. #endif
  458.  
  459.  
  460. /*  from the Stream class  */
  461. extern    object    stdoutStream_o, stdinStream_o, stderrStream_o;
  462. extern    object    traceStream_o;
  463. #define stdoutStream    (stdoutStream_o ? stdoutStream_o : (File_initialize(), stdoutStream_o))
  464. #define stdinStream    (stdinStream_o ? stdinStream_o : (File_initialize(), stdoutStream_o))
  465. #define stderrStream    (stderrStream_o ? stderrStream_o : (File_initialize(), stdoutStream_o))
  466. #define traceStream    (traceStream_o ? traceStream_o : (File_initialize(), stdoutStream_o))
  467.  
  468.  
  469.  
  470. /*  Used by Hash  */
  471.  
  472. #define BIG_INT        (sizeof(int) > 2 ? 320000001.0 : 32001.0)
  473.  
  474.  
  475. #if !defined(unix) && !defined(vms) && !defined(__WATCOMC__) && _M_IX86 < 300 && !defined (__MWERKS__)
  476. #define _HUGE    huge
  477. #else
  478. #define _HUGE
  479. #endif
  480.  
  481. /* macro to make believe a variable is used to keep picky compilers happy  */
  482. #define USE(x)        (void) x
  483.  
  484. #ifdef    __alpha
  485. #define vPrintf        fix_vPrintf
  486. #define vSprintf    fix_vSprintf
  487. #endif
  488.  
  489. #ifdef    __WATCOMC__
  490. #define    ASSIGN_VA_LIST(t,f)    memcpy(&t, &f, sizeof t)
  491. #else
  492. #define    ASSIGN_VA_LIST(t,f)    t = f
  493. #endif
  494.  
  495. #if    DPP_STRATEGY == 1
  496. #define    MAKE_REST(lst)        \
  497.     va_list    _rest_;        \
  498.     va_list _rest2_;    \
  499.     va_start(_rest_, lst);    \
  500.     ASSIGN_VA_LIST(_rest2_, _rest_)
  501. #else
  502. #define    MAKE_REST(lst)        \
  503.     va_list    _rest2_;     \
  504.     ASSIGN_VA_LIST(_rest2_, _rest_)
  505. #endif
  506.  
  507. #define    RESET_REST    ASSIGN_VA_LIST(_rest_, _rest2_)
  508.  
  509. #if    defined(__WATCOMC__)  &&  defined(__cplusplus)
  510. #pragma warning 472 9
  511. #endif
  512.  
  513. #ifdef    __cplusplus
  514. }
  515. #endif
  516.  
  517.  
  518. #endif  /*  _DYNL_H  */
  519.  
  520.  
  521. /*
  522.  *
  523.  *    Copyright (c) 1993-1996 Algorithms Corporation
  524.  *    3020 Liberty Hills Drive
  525.  *    Franklin, TN  37067
  526.  *
  527.  *    ALL RIGHTS RESERVED.
  528.  *
  529.  *
  530.  *
  531.  */
  532.  
  533.  
  534.